package com.jmc.calls;


import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.sql.SQLException;

import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400Exception;
import com.ibm.as400.access.AS400Message;
import com.ibm.as400.access.AS400SecurityException;
import com.ibm.as400.access.AS400Text;
import com.ibm.as400.access.CommandCall;
import com.ibm.as400.access.ErrorCompletingRequestException;
import com.ibm.as400.access.PrintObjectInputStream;
import com.ibm.as400.access.PrintParameterList;
import com.ibm.as400.access.ProgramCall;
import com.ibm.as400.access.ProgramParameter;
import com.ibm.as400.access.RequestNotSupportedException;
import com.ibm.as400.access.SpooledFile;

public class TestProgramSpoolCall implements Serializable{

	public static void main(String[] args) throws SQLException{
		// Call programs on system "Hal"
		AS400 as400 = new AS400("www.coinse.com.ar", "jfb", "jfb88");
		//AS400 as400 = new AS400("192.168.1.1", "jfb", "jfb88");
		//AS400 ancetre = new AS400("172.16.0.30", "user", "pass");

		SpooledFile splf = new SpooledFile(as400, "QSYSPRT", 1, "QPADEV0001", "JFB", "083954");
		PrintParameterList pl = new PrintParameterList();
		pl.setParameter(SpooledFile.ATTR_MFGTYPE, "*WSCST");
		pl.setParameter(SpooledFile.ATTR_WORKSTATION_CUST_OBJECT, "/QSYS.LIB/QWPDEFAULT.WSCST");
		InputStream in = null;
		PrintObjectInputStream pin = null;
		try {
			in = splf.getInputStream();
		} catch (AS400Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AS400SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ErrorCompletingRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RequestNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		byte[] buf = new byte[32767];
		StringBuffer sbuf = new StringBuffer();
		int bytesRead = 0;

		do {
		try {
			bytesRead = in.read( buf );
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (bytesRead > 0) {
		sbuf.append(new String(buf,1,bytesRead));
		}
		} while ( bytesRead != -1 );


		System.out.println(sbuf.toString());
	}
}
